package boncoin.handler;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import boncoin.Annonce;

public class ListAnnoncesHandler extends DefaultHandler
{
	public static String A_QNAME = "a";
	public static String DIV_QNAME = "div";
	
	private List<Annonce> annonces;
	
	private Annonce currentAnnonce = null;

	private ClassName className;
	
	int count = 0;
	
	public void startDocument() throws SAXException
	{
		super.startDocument();
		
		annonces = new ArrayList<Annonce>();
	}
	
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException
	{
		super.startElement(uri, localName, qName, attributes);
		
		if( A_QNAME.equals(qName))
		{
			currentAnnonce = new Annonce();
			currentAnnonce.setUrl(attributes.getValue("href"));
		}
		if (DIV_QNAME.equals(qName))
		{
			if (className == ClassName.date)
			{
				count++;
				if (count == 2)
				{
					className = ClassName.heure;
					count = 0;
				}
				return;
			}
			
			String clazz = attributes.getValue("class");
			
			try
			{
				className = ClassName.valueOf(clazz);
			}
			catch (Exception e) {
				className = null;
			}
		}
	}
	
	public void characters(char[] ch, int start, int length)
			throws SAXException
	{
		super.characters(ch, start, length);
		
		if (currentAnnonce == null || className == null)
			return;
		
		StringBuffer sb = buildString(ch, start, length);
		
		className.update(currentAnnonce, sb);
		
	}

	public StringBuffer buildString(char[] ch, int start, int length)
	{
		StringBuffer sb = new StringBuffer();
		for (int i = start; i < start + length; i++)
		{
			sb.append(ch[i]);
		}
		return sb;
	}
	
	public void endElement(String uri, String localName, String qName)
			throws SAXException
	{
		super.endElement(uri, localName, qName);
		
		if( A_QNAME.equals(qName))
		{
			if (currentAnnonce == null || currentAnnonce.getPrixString() == null)
				return;
			
			annonces.add(currentAnnonce);
			currentAnnonce = null;
		}
	}
	
	public void endDocument() throws SAXException
	{
		super.endDocument();
	}
	
	public void print()
	{
		for(Annonce annonce : annonces)
		{
			System.out.println(annonce.toRealString());
			System.out.println(annonce.toString());
		}
	}
	
	enum ClassName
	{
		date, 
		heure,
		category,
		title,
		price,
		placement;
		
		public void update(Annonce annonce, StringBuffer sb)
		{
			String s =sb.toString().trim();
		
			switch (this)
			{
				case category:
					s = annonce.getCategorieString() != null ? annonce.getCategorieString() + s : s;
					annonce.setCategorieString(s);
					break;
				case date:
					s = annonce.getDateString() != null ? annonce.getDateString() + s : s;
					annonce.setDateString(s);
					break;
				case heure:
					s = annonce.getHeureString() != null ? annonce.getHeureString() + s : s;
					annonce.setHeureString(s);
					break;
				case placement:
					s = annonce.getLieu() != null ? annonce.getLieu() + s : s;
					if (s != null)
					{
						StringBuffer sb2 = new StringBuffer();
						String[] lieus = s.split("/");
						for (int i = 0; i < lieus.length; i++)
						{
							if (i != 0)
								sb2.append(" / ");
							String l = lieus[i];
							sb2.append(l.trim());
						}
						s = sb2.toString();
					}
					annonce.setLieu(s);
					break;
				case price:
					s = annonce.getPrixString() != null ? annonce.getPrixString() + s : s;
					annonce.setPrixString(s);
					break;
				case title:
					s = annonce.getTitle() != null ? annonce.getTitle() + s : s;
					annonce.setTitle(s);
					break;
				default:
					break;
			}
		}
	}
}